package com.menhai.personmanagement.services;

import com.menhai.personmanagement.entity.vo.TjXdtReportVo;
import com.menhai.personmanagement.mapper.TjXdtReportMapper;
import com.menhai.personmanagement.plus.Uuid;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author shikun
 * @since 2022-05-16
 */
 @Service
public class ITjXdtReportService{

    @Value("${profile}")//文件上传路径
    String profile;

   @Autowired
   private TjXdtReportMapper tjXdtReportMapper;


   /**
    * @describe 图片
    * @param tjXdtReportVo
    * @Return int
    * @Date 2022/5/17 9:25
    * @author shikun
    */
    public int addTjXdtReport(TjXdtReportVo tjXdtReportVo){
        //新建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tjXdtReportVo.setCreateTime(sdf.format(new Date()));
        tjXdtReportVo.setType("img");
        try {
            Date date=new Date();
            String fileName ="/" + new Uuid().getUUId() + ".jpg"; //2a432ab167ae4ed6a1e82be6c4289aa7.jpg
            String dateymd ="/" +  new SimpleDateFormat("yyyy/MM/dd").format(date); // /2022/05/16/
            File file = new File(profile + dateymd); //E:/Demourl/2022/05/16
            if (!file.exists()) { //这个路径没有文件创建文件
                file.mkdirs();
            }
            FileOutputStream out = new FileOutputStream(profile + dateymd + fileName);//文件字节流输出
            out.write(Base64.decodeBase64(tjXdtReportVo.getBase64()));
            out.flush();
            out.close();
            String url = "/images" + dateymd + fileName;//结束后 拼接地址
            tjXdtReportVo.setImgsrc(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tjXdtReportMapper.insert(tjXdtReportVo);
    }

    /**
     * @describe PDF
     * @param tjXdtReportVo
     * @Return int
     * @Date 2022/5/17 9:25
     * @author shikun
     */
    public int addTjXdtReportPdf(TjXdtReportVo tjXdtReportVo){
        //新建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tjXdtReportVo.setCreateTime(sdf.format(new Date()));
        tjXdtReportVo.setType("pdf");
        try {
            Date date=new Date();
            String fileName ="/" + new Uuid().getUUId() + ".pdf"; //2a432ab167ae4ed6a1e82be6c4289aa7.pdf
            String dateymd ="/" +  new SimpleDateFormat("yyyy/MM/dd").format(date); // /2022/05/16/
            File file = new File(profile + dateymd); //E:/Demourl/2022/05/16
            if (!file.exists()) { //这个路径没有文件创建文件
                file.mkdirs();
            }
            FileOutputStream out = new FileOutputStream(profile + dateymd + fileName);//文件字节流输出
            out.write(Base64.decodeBase64(tjXdtReportVo.getBase64()));
            out.flush();
            out.close();
            String url = "/images" + dateymd + fileName;//结束后 拼接地址
            tjXdtReportVo.setPdfsrc(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tjXdtReportMapper.insert(tjXdtReportVo);
    }

    public List<TjXdtReportVo> selectTjXdtReportList(TjXdtReportVo tjXdtReportVo){
    	List<TjXdtReportVo> list=tjXdtReportMapper.selectTjXdtReportList(tjXdtReportVo);
        return list;
    }

    public int addTjXdtReportUpload(TjXdtReportVo tjXdtReportVo){
        //新建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tjXdtReportVo.setCreateTime(sdf.format(new Date()));
        tjXdtReportVo.setTestTime(sdf.format(new Date()));
        return tjXdtReportMapper.insert(tjXdtReportVo);
    }
}
